package com.example.healkep;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkep.MainActivity.MainActivity;
import com.example.healkep.View.health.healthActivity;
import com.example.healkep.bean.globalUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainUser extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_main);
        //view层的控件和业务层的控件，靠id关联和映射  赋值，即设置布局文件中的Button按钮id进行关联
        Button select1 = findViewById(R.id.select1);
        Button select2 = findViewById(R.id.select2);
        Button select3 = findViewById(R.id.select3);
        Button userinform = findViewById(R.id.button4);
        Button Logout = findViewById(R.id.button5);
        Button userdiseases = findViewById(R.id.button6);
        Button d_btn = findViewById(R.id.button7);
        TextView userid = findViewById(R.id.userid);
        String json1 = "{\n" +
                "    \"username\":\"" + globalUserInfo.getInstance().getUsername() + "\"\n" +
                "}";
        new Thread(() -> {
            try {
                OkHttpClient clientget = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(LoginActivity.url+"/information/get")
                        .post(RequestBody.create(MediaType.parse("application/json"), json1))
                        .build();
                Response response = clientget.newCall(request).execute();
                String resultget = Objects.requireNonNull(response.body()).string();
                JSONObject jsonObject = new JSONObject(resultget);
                runOnUiThread(()->{
                    try {
                        userid.setText(jsonObject.getString("clientName"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                });
            }catch (IOException | JSONException e){
                Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
            }
        }).start();
        userid.setText("");


        d_btn.setOnClickListener(view -> {
            Intent intent7 = new Intent(MainUser.this,DoctorListActivity.class);
            startActivity(intent7);
        });

        //绑定监听事件
        select1.setOnClickListener(view -> {
            if (view.getId() == R.id.select1) {
                // 添加点击响应事件
                Intent intent1 = new Intent(MainUser.this, MainActivity.class);
                //启动
                startActivity(intent1);
            }
        });

        select2.setOnClickListener(view -> {
            if (view.getId() == R.id.select2) {
                // 给bnt1添加点击响应事件
                Intent intent2 = new Intent(MainUser.this, FoodMainActivity.class);
                //启动
                startActivity(intent2);
            }
        });

        select3.setOnClickListener(view -> {
            if (view.getId() == R.id.select3) {
                // 添加点击响应事件
                Intent intent3 = new Intent(MainUser.this, MainBg.class);
                //启动
                startActivity(intent3);
            }
        });
        Button select5 = findViewById(R.id.select5);
        select5.setOnClickListener((view)->{
            Intent intent1 = new Intent ( MainUser.this, CameraActivity.class );
            startActivity ( intent1 );
        });
        userinform.setOnClickListener(view -> {
            if (view.getId() == R.id.button4) {
                Intent intent4 = new Intent();
                intent4.setClass(MainUser.this, InformationActivity.class);
                startActivity(intent4);
            }
        });

        userdiseases.setOnClickListener(view ->{
            if(view.getId() == R.id.button6){
                Intent intent6 = new Intent(MainUser.this, healthActivity.class);
                startActivity(intent6);
            }
        });

        Logout.setOnClickListener(view -> {//借用来测试index
            //Intent intent = new Intent(MainUser.this,IndexgetActivity.class);
            //startActivity(intent);
            String username = LoginActivity.getUser();
            String sessionid = LoginActivity.getSessionID();
            String json = "{\n" +
                    "    \"username\":\"" + username + "\"\n" +
                    "}";
            new Thread(() -> {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(LoginActivity.url + "/logout/logout")
                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                            .addHeader("Cookie", sessionid)
                            .build();
                    Response response = client.newCall(request).execute();
                    String result = Objects.requireNonNull(response.body()).string();
                    JSONObject jsonObject = new JSONObject(result);
                    String user = jsonObject.getString("username");
                    if (user.equals("!@#")) {
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(MainUser.this, LoginActivity.class);
                        startActivity(intent);
                        runOnUiThread(() -> Toast.makeText(MainUser.this, "该用户已退出!", Toast.LENGTH_SHORT).show());
                    } else if (user.equals("!@!")) {
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(MainUser.this, LoginActivity.class);
                        startActivity(intent);
                        runOnUiThread(() -> Toast.makeText(MainUser.this, "该用户退出完成", Toast.LENGTH_SHORT).show());
                    } else if(user.equals("###")) {
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(MainUser.this, LoginActivity.class);
                        startActivity(intent);
                        runOnUiThread(() -> Toast.makeText(MainUser.this, "该用户已退出!!", Toast.LENGTH_SHORT).show());
                    } else{
                        Intent intent = new Intent();
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setClass(MainUser.this, LoginActivity.class);
                        startActivity(intent);
                        runOnUiThread(() -> Toast.makeText(MainUser.this, "异常", Toast.LENGTH_SHORT).show());
                    }
                } catch (Exception e) {
                    Intent intent = new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(MainUser.this, LoginActivity.class);
                    startActivity(intent);
                    runOnUiThread(() -> Toast.makeText(MainUser.this, "连接异常", Toast.LENGTH_SHORT).show());
                }
            }).start();
        });
    }
}
